//
//  CJSTravelNoteCell.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/25.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSTravelNoteCell.h"
#import "UIImageView+WebCache.h"
#import "WSTools.h"
#import "GSYImageView.h"

#define kHEIGHT 0.51 * SCREEN_HEIGHT

@interface CJSTravelNoteCell ()

@property (nonatomic, retain) UIImageView *headImage;

@property (nonatomic, retain) UILabel *contentLabel;

@property (nonatomic, retain) UIImageView *timeImage;

@property (nonatomic, retain) UILabel *time;

@property (nonatomic, retain) UIView *cellView;

@property (nonatomic, retain) UIImageView *navImage;

@property (nonatomic, retain) GSYImageView *occupyImage;

@end

@implementation CJSTravelNoteCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    
    self.cellView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, 410 * ADAPT_HEIGHT)];
    [self.contentView addSubview:self.cellView];
    
    self.occupyImage = [[GSYImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, kHEIGHT)];
    [self.cellView addSubview:self.occupyImage];
    
    //图片
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, kHEIGHT)];
    self.headImage.contentMode = UIViewContentModeScaleAspectFill;
    self.headImage.layer.masksToBounds = YES;
    [self.cellView addSubview:self.headImage];
    
    //内容
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.headImage.frame.size.width - 20, 20)];
    self.contentLabel.textColor = [UIColor colorWithRed:0.48 green:0.48 blue:0.48 alpha:1];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.font = [UIFont systemFontOfSize:15];
    [self.cellView addSubview:self.contentLabel];
    
    self.timeImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.contentLabel.frame.origin.y + 30, 16, 16)];
    
    self.timeImage.layer.cornerRadius = 8;
    self.timeImage.layer.masksToBounds = YES;
    [self.cellView addSubview:self.timeImage];
    
    //拍摄时间
    self.time = [[UILabel alloc] initWithFrame:CGRectMake(self.timeImage.frame.origin.x + 25, self.contentLabel.frame.origin.y + 30, 200, 16)];
    
    self.time.font = [UIFont systemFontOfSize:14];
    self.time.textColor = [UIColor colorWithRed:0.65 green:0.65 blue:0.65 alpha:1];
    [self.cellView addSubview:self.time];
    
    self.navImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * ADAPT_WIDTH, self.cellView.frame.size.height, SCREEN_WIDTH - 20, 20)];
    [self.contentView addSubview:self.navImage];
}

#pragma mark - model的set方法
- (void)setWaypoints:(CJSWaypoints *)waypoints {
    
    _waypoints = waypoints;
    
    CGFloat height = [WSTools heightForContent:self.waypoints.text size:15];
    
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:self.waypoints.photo] placeholderImage:[UIImage imageNamed:@""]];
    if ([self.waypoints.photo isEqualToString:@""]) {
        CGRect headFrame = self.headImage.frame;
        headFrame.size.height = 0;
        self.headImage.frame = headFrame;
        self.occupyImage.hidden = YES;
    }
    else {
        
        CGRect headFrame = self.headImage.frame;
        headFrame.size.height = kHEIGHT;
        self.headImage.frame = headFrame;
        self.occupyImage.hidden = NO;
    }
    CGFloat headHeight = self.headImage.frame.size.height;
    
    CGRect frame = self.cellView.frame;
    frame.size.height = 410 * ADAPT_HEIGHT + height - 20 - kHEIGHT + headHeight;
    self.cellView.frame = frame;
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.cellView.backgroundColor = [UIColor colorWithRed:0.18 green:0.21 blue:0.24 alpha:1];
    }
    else {
        
        self.cellView.backgroundColor = [UIColor whiteColor];
    }
    
    self.contentLabel.text = self.waypoints.text;
    frame = self.contentLabel.frame;
    frame.origin.y = 10 + headHeight;
    frame.size.height = height;
    self.contentLabel.frame = frame;
    
    self.timeImage.image = [UIImage imageNamed:@"clock_gray_big"];
    frame = self.timeImage.frame;
    frame.origin.y = headHeight + height + 20;
    self.timeImage.frame = frame;
    
    self.time.text = self.waypoints.local_time;
    frame = self.time.frame;
    frame.origin.y = headHeight + height + 20;
    self.time.frame = frame;
    
    self.navImage.image = [UIImage imageNamed:@"waypoint_cell_shadow"];
    frame = self.navImage.frame;
    frame.origin.y = 410 * ADAPT_HEIGHT - kHEIGHT + headHeight + height - 20;
    self.navImage.frame = frame;
    
    NSLog(@"======%@",self.time);
    
}

- (void)setNothing {
    
    self.timeImage.image = [UIImage imageNamed:@""];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
